﻿#include "geodrawwidget.h"
#include <assert.h>
#include<QPainter>
#include<QPen>
#include<QMouseEvent>
#include "../geoprojectmanager.h"
GeoDrawWidget::GeoDrawWidget(QWidget *parent) : QWidget(parent)
{

}

void GeoDrawWidget::initPainter(QPainter *painter) const
{

}

void GeoDrawWidget::mousePressEvent(QMouseEvent *e)
{
    GeoProject* proj = GPRO.CurProj();
    GeoProject::OPMode opmode =  proj->m_omode;
    switch(opmode)
    {
    case GeoProject::OPMode::SELECT_MODE:
        break;
    case GeoProject::OPMode::DRAW_MODE:
    {

        //如果是画画模式的话就要看下最近的状态
        DataType type = proj->m_drawMode;
        switch(type)
        {
            case TYPE_LINE:
                {
                        LineData *ld = new LineData;
                        ld->pos[0].pos.x = ld->pos[1].pos.x = e->x();
                        ld->pos[0].pos.y = ld->pos[1].pos.y = e->y();
                        proj->gm.AddData(ld);
                }
                break;
            case TYPE_CIRCLE:
                {
                        CircleData *cd = new CircleData;
                        cd->center.pos.x = e->x();
                        cd->center.pos.y = e->y();
                        proj->gm.AddData(cd);
                }
            break;
            case TYPE_EDGE:
               {
                   //先不实现
                }
            break;
        }
    }
        break;
    default:
        break;
    }
    update();
}

void GeoDrawWidget::mouseReleaseEvent(QMouseEvent *e)
{
    GeoProject* proj = GPRO.CurProj();
    GeoProject::OPMode opmode =  proj->m_omode;
    switch(opmode)
    {
    case GeoProject::OPMode::SELECT_MODE:
        break;
    case GeoProject::OPMode::DRAW_MODE:
    {
        //如果是画画模式的话就要看下最近的状态
        DataType type = proj->m_drawMode;
        switch(type)
        {
            case TYPE_LINE:
                {
                    if(proj->gm.CurData())
                    {
                        LineData *ld = (LineData*)proj->gm.CurData();
                        ld->pos[1].pos.x = e->x();
                        ld->pos[1].pos.y = e->y();
                    }
                 }
                break;
            case TYPE_CIRCLE:
                {
                    CircleData *cd = (CircleData*)proj->gm.CurData();
                    int rad = e->x() - cd->center.pos.x;
                    cd->radius =  rad > 0 ? rad : -rad;
                }
            break;
            case TYPE_EDGE:
               {
                   //先不实现
                }
            break;
        }
    }
        break;
    default:
        break;
    }
    update();
}

void GeoDrawWidget::mouseMoveEvent(QMouseEvent *e)
{
    GeoProject* proj = GPRO.CurProj();
    GeoProject::OPMode opmode =  proj->m_omode;
    switch(opmode)
    {
    case GeoProject::OPMode::SELECT_MODE:
        break;
    case GeoProject::OPMode::DRAW_MODE:
    {
        //如果是画画模式的话就要看下最近的状态
        DataType type = proj->m_drawMode;
        switch(type)
        {
            case TYPE_LINE:
                {
                    if(proj->gm.CurData())
                    {
                        assert(proj->gm.GetData().size()!=0);
                        LineData *ld = (LineData*)proj->gm.CurData();
                        ld->pos[1].pos.x = e->x();
                        ld->pos[1].pos.y = e->y();
                    }
                 }
                break;
            case TYPE_CIRCLE:
                {
                    CircleData *cd = (CircleData*)proj->gm.CurData();
                    int rad = e->x() - cd->center.pos.x;
                    cd->radius =  rad > 0 ? rad : -rad;
                }
            break;
            case TYPE_EDGE:
               {
                   //先不实现
                }
            break;
        }
        break;
    }
    default:
        break;
    }
    update();
}

void GeoDrawWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPen pen = painter.pen();
    GeoProject* pro = GPRO.CurProj();
    vector<Data*>& data = pro->gm.GetData();
    for(int i=0;i<data.size();++i)
    {
        switch(data[i]->type)
        {
            case TYPE_LINE:
                {
                    LineData* ld = (LineData*)data[i];
                    QPoint p1(ld->pos[0].pos.x,ld->pos[0].pos.y);
                    QPoint p2(ld->pos[1].pos.x,ld->pos[1].pos.y);
                   // pen.setWidth(ld->lineWidth);//改变线宽
                    painter.setPen(pen);
                    painter.drawLine(p1,p2);
                }
                break;
            case TYPE_CIRCLE:
                {
                    CircleData* cd = (CircleData*)data[i];
                    QPoint center(cd->center.pos.x,cd->center.pos.y);
                    painter.drawEllipse(center.x(),center.y(),cd->radius,cd->radius);
                }
            break;
            case TYPE_EDGE:
               {
                   //先不实现
                }
            break;
        }
    }

}

void GeoDrawWidget::resizeEvent(QResizeEvent *)
{

}

